package com.cloudOffice.helper;

import com.cloudOffice.model.system.SysMenu;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @program: cloud-office
 * @className: MenuHelper
 * @description: 构建菜单数据
 * @data: 2024/2/8 14:55
 * @author: ihu
 * @version: 1.0
 **/

public class MenuHelper {
	
	/**
	 * 使用递归方法建菜单
	 */
	public static List<SysMenu> buildTree(List<SysMenu> sysMenuList) {
		return sysMenuList.stream().filter(e -> e.getParentId() == 0).map(e -> findChildren(e, sysMenuList)).collect(Collectors.toList());
	}
	
	/**
	 * 递归查找子节点
	 */
	public static SysMenu findChildren(SysMenu sysMenu, List<SysMenu> treeNodes) {
		if (sysMenu.getChildren() == null) {
			sysMenu.setChildren(new ArrayList<>());
		}
		treeNodes.stream().filter(e -> e.getParentId().equals(sysMenu.getId())).forEach(e -> sysMenu.getChildren().add(findChildren(e, treeNodes)));
		return sysMenu;
	}
	
}
